<?php
/*
网页授权登入
 *  */
require_once 'include/config.php';
require_once 'include/http.php';

$wechat = new author();

class author{
    
    private $appid = '';
    private $appsecret = '';
    private $http = '';
    public function __construct() {
        $this->appid = constant('APPID');
        $this->appsecret = constant('APPSECRET');
        $this->http = new http();
        //获取接口
        $type = isset($_GET['type'])?$_GET['type']:'';
        switch ($type) {
            case 'get_code':
                $ret = $this->getCode();
                break;
            default:
                $info = $this->getAccessToken();
                $ret = $this->getUserInfo($info);
                echo "<pre>";
                print_r($ret);
                exit;
                break;
        }
    }
    //获取code
    private function getCode() {
        //重定向页面
        $redirect_uri = "http://d44whp.natappfree.cc/author.php";
        //请求连接
        $url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->appid}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state=STATE";
        //跳转链接
        header("Location: {$url}"); 
    }
    //获取access_token
    private function getAccessToken() {
        $code = $_GET['code'];
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->appid}&secret={$this->appsecret}&code={$code}&grant_type=authorization_code";
        $access_token = $this->http->http_request($url);
        $info = json_decode($access_token,true);
        return $info;
    }
    //获取User具体信息
    private function getUserInfo($info) {
        $url = "https://api.weixin.qq.com/sns/userinfo?access_token={$info['access_token']}&openid={$info['openid']}&lang=zh_CN";
        $userinfo = $this->http->http_request($url);
        $userinfo = json_decode($userinfo,true);
        return $userinfo;
    }
}

